package tech.yunjing.train.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.yunjing.train.common.Constants;
import tech.yunjing.train.dao.AccountDetailDao;
import tech.yunjing.train.dao.ReceiveRedPacketRecordRepository;
import tech.yunjing.train.entity.AccountDetail;
import tech.yunjing.train.entity.ReceiveRedPacketRecord;
import tech.yunjing.train.entity.UserInfo;
import tech.yunjing.train.service.LoginService;
import tech.yunjing.train.service.ReceiveRedPacketRecordService;
import tech.yunjing.train.service.exception.ReceiveRedPacketRecordDeleteException;
import tech.yunjing.train.service.exception.ReceiveRedPacketRecordException;
import tech.yunjing.train.service.exception.UserAccountException;
import tech.yunjing.train.service.exception.UserLoginException;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * 红包记录serviceImpl
 * Created by zlc on 2017/6/10.
 */
@Service
@Transactional
public class ReceiveRedPacketRecordServiceImpl implements ReceiveRedPacketRecordService {

    @Autowired
    private ReceiveRedPacketRecordRepository receiveRedPacketRecordRepository;
    @Autowired
    private LoginService<UserInfo> loginService;
    @Autowired
    private AccountDetailDao accountDetailDao;

    /**
     * 根据用户账户查询所有的抢红包记录
     *
     * @param userAccount 用户账户
     * @return 查询结果
     */
    public List<ReceiveRedPacketRecord> findByUserAccount(String userAccount) {
        //判断用户账户
        if (userAccount == null || userAccount.trim().isEmpty()) {
            throw new UserAccountException("账户错误");
        }
        //判断用户是否登录
        //根据账号查询该用户
        UserInfo userInfo = loginService.getUserAccount(userAccount);
        if (userInfo == null) {
            throw new UserLoginException("请登录！");
        }
        //获取记录
        List<ReceiveRedPacketRecord> list = receiveRedPacketRecordRepository.findByReceiveAccountAndLogicDelete(userAccount, Constants.DEL_LOGIC_NORMAL);
        //判断结果
        if (list.isEmpty()) {
            throw new ReceiveRedPacketRecordException("没有记录！");
        }
        return list;
    }

    /**
     * 将给定的红包记录集合保存至账户明细中
     *
     * @param list        红包记录集合
     * @param userAccount 账户名
     * @return 保存的记录数
     */
    public int saveAccountDetail(List<ReceiveRedPacketRecord> list, String userAccount) {
        int count = 0;
        //判断记录是否被保存至明细中
        for (ReceiveRedPacketRecord receiveRedPacketRecord : list) {
            //未保存则保存至明细中
            if (Constants.FIND_STATE_NORMAL.equals(receiveRedPacketRecord.getFindState())) {
                //创建账户明细记录
                AccountDetail accountDetail = new AccountDetail();
                //设置明细记录的账户
                accountDetail.setAccountName(userAccount);
                //设置明细发生时间
                accountDetail.setHappendTime(receiveRedPacketRecord.getReceiveDate());
                //设置明细金额
                accountDetail.setHappendMoney(receiveRedPacketRecord.getReceiveMoney());
                //将明细记录保存至数据库中
                accountDetailDao.addAccountDetail(accountDetail);
                //获取记录id
                String id = receiveRedPacketRecord.getReceiveRedPacketId();
                //根据记录Id更新记录状态
                receiveRedPacketRecordRepository.updateFindState(Constants.FIND_STATE_ALREADY, new Date(), id);
                count ++;
            }
        }
        return count;
    }


    /**
     * 根据指定账户逻辑删除其所有的抢红包记录
     *
     * @param userAccount 用户账户
     * @return 删除的记录数
     */
    @Override
    public int logicDelete(String userAccount) {
        //判断用户账户
        if (userAccount == null || userAccount.trim().isEmpty()) {
            throw new UserAccountException("账户错误");
        }
        //判断用户是否登录
        //根据账号查询该用户
        UserInfo userInfo = loginService.getUserAccount(userAccount);
        if (userInfo == null) {
            throw new UserLoginException("请登录！");
        }
        //获取该用户的所有未删除记录
        List<ReceiveRedPacketRecord> list = receiveRedPacketRecordRepository.findByReceiveAccountAndLogicDelete(userAccount, Constants.DEL_LOGIC_NORMAL);
        //判断结果
        if (list.isEmpty()) {
            return 0;
        }
        //调用保存明细方法
        this.saveAccountDetail(list,userAccount);
        //将记录逻辑删除
        int a = receiveRedPacketRecordRepository.logicDelete(Constants.DEL_LOGIC_DELETE, new Date(), userAccount, Constants.DEL_LOGIC_NORMAL);
        if (list.size() != a) {
            throw new ReceiveRedPacketRecordDeleteException("删除失败");
        }
        return a;
    }
}
